;(function () {
    var table = function (ele, opt) {

        this.$element = ele,
            defaults = {
                "processing": true,
                "serverSide": true,
                "lengthMenu": [[10, 20, 50, 100, 200], [10, 20, 50, 100, 200]],
                "pageLength": 10,
                "searching": false,
                "order": [],
                "dom": '<"top">rt<"page"filp><"clear">',
                "oLanguage": {    // 语言设置
                    "sLengthMenu": "每页显示 _MENU_ 条记录",
                    "sInfo": "本页数据从 _START_ 到 _END_ /共 _TOTAL_ 条数据",
                    "sInfoEmpty": "没有数据",
                    "sInfoFiltered": "(从 _MAX_ 条数据中检索)",
                    "sZeroRecords": "没有数据",
                    "sSearch": "检索:",
                    "oPaginate": {
                        "sFirst": "首页",
                        "sPrevious": "上一页",
                        "sNext": "下一页",
                        "sLast": "尾页"
                    }
                },
                "scrollX": "",
                "select": false,//多选,只是选中效果
                "oneSelect": false,//单选，只是选中效果
                "selectCheck": false,//多选,同时选中复选框
                "oneSelectCheck": false,//单选，同时选中复选框
                "visible": true,//隐藏显示列
                "getObj": '',//初始化生成的表格对象
                "columnDefs": [
                    {
                        sDefaultContent: '',
                        aTargets: ['_all']
                    }
                ],
                "fnDrawCallback": function (oSettings) {
                    $('.table-menu').remove();
                    var tableId = oSettings.sTableId,
                        _this = $('#' + oSettings.sTableId);
                    var fixLeft, fixRig;
                    var thLen = oSettings.aoColumns.length;
                    var menuHtm = '<div class="table-menu"><label class="checkbox-label"><input type="checkbox" checked="checked" class="menuAll">全选</label>';
                    if (oSettings.oScroll.sX == "true" || oSettings['_oFixedColumns'] != undefined) {
                        if (oSettings['_oFixedColumns'] != undefined) {
                            fixLeft = oSettings['_oFixedColumns'].s.iLeftColumns;
                            fixRig = oSettings['_oFixedColumns'].s.iRightColumns;
                            for (var i = 0; i < oSettings.aoColumns.length; i++) {
                                if (i > fixLeft - 1 && i < thLen - fixRig) {
                                    var isVisible = oSettings.aoColumns[i].bVisible;
                                    if (isVisible) {
                                        menuHtm += '<label class="checkbox-label"><input type="checkbox" value="' + i + '" name="menuHtm" checked="checked"/><span>' + oSettings.aoColumns[i].sTitle + '</span></label>'
                                    } else {

                                        menuHtm += '<label class="checkbox-label"><input type="checkbox" value="' + i + '" name="menuHtm"/><span>' + oSettings.aoColumns[i].sTitle + '</span></label>'
                                    }
                                }
                            }
                        } else {
                            for (var i = 0; i < oSettings.aoColumns.length; i++) {
                                var isVisible = oSettings.aoColumns[i].bVisible;
                                var name = $(this).text();
                                var title = oSettings.aoColumns[i].sTitle;
                                if (!(new RegExp('type="checkbox"').test(title))) {
                                    if (isVisible) {
                                        menuHtm += '<label class="checkbox-label"><input type="checkbox" value="' + i + '" name="menuHtm" checked="checked"/><span>' + oSettings.aoColumns[i].sTitle + '</span></label>'
                                    } else {

                                        menuHtm += '<label class="checkbox-label"><input type="checkbox" value="' + i + '" name="menuHtm"/><span>' + oSettings.aoColumns[i].sTitle + '</span></label>'
                                    }
                                }
                            }
                            ;
                        }

                    } else {
                        for (var i = 0; i < oSettings.aoColumns.length; i++) {
                            var isVisible = oSettings.aoColumns[i].bVisible;
                            var name = $(this).text();
                            var title = oSettings.aoColumns[i].sTitle;
                            if (!(new RegExp('type="checkbox"').test(title))) {
                                if (isVisible) {
                                    menuHtm += '<label class="checkbox-label"><input type="checkbox" value="' + i + '" name="menuHtm" checked="checked"/><span>' + oSettings.aoColumns[i].sTitle + '</span></label>'
                                } else {

                                    menuHtm += '<label class="checkbox-label"><input type="checkbox" value="' + i + '" name="menuHtm"/><span>' + oSettings.aoColumns[i].sTitle + '</span></label>'
                                }
                            }
                        }
                        ;
                    }
                    menuHtm += '</div>';
                    $('body').append(menuHtm);
                    var oTable = $("#" + oSettings.sTableId).dataTable();
                    if (oTable.api().page.info().pages > 1) {
                        var cuPage = $("#" + oSettings.sTableId + "_paginate").find(".current").text();
                        $("#" + oSettings.sTableId + "_paginate").append('&nbsp;&nbsp;&nbsp;跳转到第<input type="number" id="jump_page" onblur="jumpPage(this,\'' + oSettings.sTableId + '\');" class="paginate_button" min="1" max="' + oTable.api().page.info().pages + '" value="' + cuPage + '" style="width: 50px;height: 24px;border: 1px solid #989898;margin: 0px 2px;padding: 2px;cursor: auto;" title="鼠标离开输入框点击空白处即可跳转">页');
                    }
                    tableSize();
                },
            }
        this.options = $.extend(defaults, opt);
        // console.log(this.options);
    };
    var tableObj;
    table.prototype = {
        start: function (table) {
            var w = $(window).height();
            var _this = $(this.$element);
            var visible = this.options.visible;
            $(this.$element).attr("width", "100%");
            // debugger;
            if (this.options.sScrollY == true) {
                var head = $(this.$element).find('thead').height();
                var wH = $(window).height(),
                    t = $(this.$element).offset().top,
                    tH = wH - t - 51 - head - 10;
                this.options.sScrollY = tH;
                tableObj = $(this.$element).DataTable(this.options);
            } else {
                tableObj = $(this.$element).DataTable(this.options);
            }
            var outer = $("#" + $(this.$element)[0].id + "_wrapper");

            if (visible == true) {
                outer.find('thead th').bind("contextmenu", function (e) {
                    return false;
                });
                outer.find('thead th').mousedown(function (event) {
                    var sty = {
                        left: document.body.scrollLeft + event.clientX,
                        top: document.body.scrollTop + event.clientY
                    }
                    if (event.which == 3) {
                        $('.table-menu').show();
                        $('.table-menu').css(sty);
                        /**
                         * 取消浏览器右键框
                         */
                        $(document).bind("contextmenu", function (e) {
                            return false;
                        });
                    } else {
                        $('.table-menu').hide();
                    }
                });
            }
            //选中行操作
            var select = this.options.select,
                oneSelect = this.options.oneSelect,
                selectCheck = this.options.selectCheck,
                oneSelectCheck = this.options.oneSelectCheck,
                scrollX = this.options.scrollX;
            $(this.$element).find('tbody').on('click', 'tr', function () {
                var obj = $(this);
                var rowNum = tableObj.row(this)[0][0];
                if (select != false) {
                    var isCheck = $(this).hasClass('selected');
                    if (isCheck) {
                        $(this).removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_RightBodyWrapper tbody tr').eq(rowNum).removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('tbody tr').eq(rowNum).removeClass('selected');
                    } else {
                        $(this).addClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_RightBodyWrapper tbody tr').eq(rowNum).addClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).addClass('selected');
                        $(this).parents('.dataTables_wrapper').find(' tbody tr').eq(rowNum).addClass('selected');
                    }
                }
                if (oneSelect != false) {
                    if ($(this).hasClass('selected')) {
                        $(this).removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_RightBodyWrapper tbody tr').eq(rowNum).removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('tbody tr').eq(rowNum).removeClass('selected');
                    }
                    else {
                        _this.find('tr.selected').removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('tbody tr').removeClass('selected');
                        $(this).addClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).addClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_RightBodyWrapper tbody tr').eq(rowNum).addClass('selected');
                        $(this).parents('.dataTables_wrapper').find('tbody tr').eq(rowNum).addClass('selected');
                    }
                }
                if (selectCheck != false) {
                    $(this).toggleClass('selected');
                    var isCheck = $(this).hasClass('selected');
                    $(this).find('[type="checkbox"]').prop("checked", isCheck);
                    $(this).parents('.dataTables_wrapper').find('tbody tr').eq(rowNum).find('[type="checkbox"]').prop("checked", isCheck);
                    $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).find('[type="checkbox"]').prop("checked", isCheck);
                    $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).toggleClass('selected');
                    $(this).parents('.dataTables_wrapper').find('.DTFC_RightBodyWrapper tbody tr').eq(rowNum).toggleClass('selected');
                }
                if (oneSelectCheck != false) {
                    if ($(this).hasClass('selected')) {
                        $(this).removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_RightBodyWrapper tbody tr').eq(rowNum).removeClass('selected');
                        $(this).parents('.dataTables_wrapper').find('tbody tr').eq(rowNum).removeClass('selected');
                        var isCheck = $(this).hasClass('selected');
                        $(this).find('[type="checkbox"]').prop("checked", isCheck);
                        $(this).parents('.dataTables_wrapper').find('tbody tr').eq(rowNum).find('[type="checkbox"]').prop("checked", isCheck);
                        $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).find('[type="checkbox"]').prop("checked", isCheck);
                    }
                    else {
                        _this.parents('.dataTables_wrapper').find('tr.selected').removeClass('selected');
                        $(this).addClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).addClass('selected');
                        $(this).parents('.dataTables_wrapper').find('.DTFC_RightBodyWrapper tbody tr').eq(rowNum).addClass('selected');
                        $(this).parents('.dataTables_wrapper').find('tbody tr').eq(rowNum).addClass('selected');
                        _this.parents('.dataTables_wrapper').find('tbody tr').find('[type="checkbox"]').prop("checked", false);
                        $(this).find('[type="checkbox"]').prop("checked", true);
                        $(this).parents('.dataTables_wrapper').find('tbody tr').eq(rowNum).find('[type="checkbox"]').prop("checked", true);
                        $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyWrapper tbody tr').eq(rowNum).find('[type="checkbox"]').prop("checked", true);
                    }

                }
            });
            //点击桌面关闭隐藏列层
            $('body').on('mousedown', function (event) {
                if (!($(event.target).is($('.table-menu').find('*'))) && !($(event.target).is(outer.find('thead th')))) {
                    $('.table-menu').hide();
                    $(document).unbind("contextmenu", "")
                }
            });
            //隐藏显示列
            $(document).on("change", ".menuAll", function () {
                var checked = $(this).prop("checked");
                var that = $(this).parent().nextAll(".checkbox-label").find("input[type='checkbox']");
                if (!checked) {
                    that.each(function () {
                        var check = $(this).prop("checked");
                        if (check) {
                            var _thisVal = $(this).val();
                            tableObj.column(_thisVal).visible(false);
                            $(this).prop("checked", false);
                        }
                    });
                } else {
                    that.each(function () {
                        var check = $(this).prop("checked");
                        if (!check) {
                            var _thisVal = $(this).val();
                            tableObj.column(_thisVal).visible(true);
                            $(this).prop("checked", true);
                        }
                    });
                }
                ;

            });
            $(document).on('change', '.table-menu input', function (e) {
                var column = tableObj.column($(this).val());
                var hasCheck = $(this).prop("checked");
                if (!hasCheck) {
                    column.visible("");
                    $('.dataTables_scrollBody tbody').children('tr').each(function (i) {
                        $(this).css("height", "auto");
                        var trHei = $(this).height();
//						console.log(trHei)
                        $(this).parents('.dataTables_wrapper').find('.DTFC_LeftBodyLiner tbody').children('tr').eq(i).height(trHei)
                        $(this).parents('.dataTables_wrapper').find('.DTFC_RightBodyLiner tbody').children('tr').eq(i).height(trHei)
                    });
                    $(".menuAll").prop("checked", false);
                } else {
                    column.visible(!column.visible());
                }
                tableSize();
            });
            /**
             * 取消浏览器右键框
             */
//	        document.oncontextmenu = function (obj) {
//	            return false;
//	        };
            //窗口大小改变的时候，重绘表格
            if (scrollX == 'true') {
                window.onresize = function () {
                    tableSize();
                }
            }
            return tableObj;
        },
        getCheck: function () {
            var getTable = $(this.$element).DataTable();
            var data1 = getTable.rows('.selected').data();
            var resData = [];
            // $.each(data1,function(i,vals){
            //     resData.push(vals)
            // })
            $(this.$element).find('.checkchild').each(function () {
                var that = $(this);
                if ($(this).is(':checked')) {
                    var rowList = that.parents('tr');
                    var data = getTable.row(rowList).data();
                    resData.push(data)
                }
            });
            return resData;
        }, getData: function () {
            var getTable = $(this.$element).DataTable();
            var data1 = getTable.data();
//			var data1=tableObj.data();
            var resData = [];
            $.each(data1, function (i, vals) {
                resData.push(vals)
            })
            return resData;
        },
        reload: function () {
            tableObj.ajax.reload();

        },
        reloadFalse: function () {
            tableObj.ajax.reload(null, false);

        }
    };
    $.fn.dataTableExtend = function (options) {
        var dataTables = new table(this, options);
        if (options == "getCheck") {
            return dataTables.getCheck();
        } else if (options == "getData") {
            return dataTables.getData();
        } else if (options == "reload") {
            return dataTables.reload();
        } else if (options == "reloadFalse") {
            return dataTables.reloadFalse();
        } else {
            return dataTables.start();
        }
    }
})();
$(document).ready(function () {
    //全选
    $(document).on('change', '.checkall', function () {
        var isCheck = $(this).prop("checked");
        $(this).parents('.dataTables_wrapper,table').find('tbody').find('.checkchild').prop("checked", isCheck);
        if (isCheck) {
            $(this).parents('.dataTables_wrapper,table').find('tbody tr').addClass('selected');
        } else {
            $(this).parents('.dataTables_wrapper,table').find('tbody tr').removeClass('selected');
        }
    })
    $(document).on('change', '.checkchild', function () {
        var isCheck = $(this).prop("checked");
        var num = $(this).parents('tr').attr('data-dt-row');
        if (!isCheck) {
            $(this).parents('tr').removeClass('selected');
            $(this).parents('.dataTables_wrapper').find('.dataTables_scrollBody tbody').children().eq(num).removeClass('selected');
            $(this).parents('.dataTables_wrapper').find('.dataTables_scrollBody tbody').children().eq(num).find('input').prop('checked', false);
            $(this).parents('table').find('.checkall').prop("checked", false)
        } else {
            $(this).parents('tr').addClass('selected');
            $(this).parents('.dataTables_wrapper').find('.dataTables_scrollBody tbody').children().eq(num).find('input').prop('checked', true);
            $(this).parents('.dataTables_wrapper').find('.dataTables_scrollBody tbody').children().eq(num).addClass('selected');
            $(this).parents('.dataTables_wrapper').find('.dataTables_scrollBody tbody').children().eq(num).find('input').prop('checked', true);
        }
    });
});

function tableSize() {
    //列宽
    var thLen = $('.dataTables_scrollBody table>thead>tr>th').length;
    var thLefLen = $('.DTFC_LeftBodyWrapper table>thead>tr>th').length;
    var thRigLen = $('.DTFC_RightBodyWrapper table>thead>tr>th').length;
    $('.dataTables_scrollBody table>thead>tr>th').each(function (i) {
        var thWin = $(this).width();
        $('.dataTables_scrollBody table>thead>tr>th').eq(i).css("width", thWin);
        if (i < thLefLen) {
            $('.DTFC_LeftHeadWrapper table>thead>tr>th').eq(i).css("width", thWin);
            $('.DTFC_LeftBodyWrapper table>thead>tr>th').eq(i).css("width", thWin);
        } else if (i >= thLen - thRigLen) {
            $('.DTFC_RightHeadWrapper table>thead>tr>th').eq(i).css("width", thWin);
            $('.DTFC_RightBodyWrapper table>thead>tr>th').eq(i).css("width", thWin);
        }
    });
    //列高
    $('.dataTables_scrollBody tbody').children('tr').each(function (i) {
        $(this).css("height", "auto");
        var trHei = $(this).height();
        $('.DTFC_LeftBodyLiner tbody').children('tr').eq(i).height(trHei)
        $('.DTFC_RightBodyLiner tbody').children('tr').eq(i).height(trHei);
    });
    //高度计算
    var fixLef = $('.DTFC_LeftBodyWrapper table').height(),
        fixRig = $('.DTFC_RightBodyWrapper table').height(),
        mainHei = $('.dataTables_scroll').height();
//	console.log(fixLef)
    $('.DTFC_LeftBodyWrapper,.DTFC_LeftBodyWrapper>div').css("height", fixLef);
    $('.DTFC_RightBodyWrapper,.DTFC_RightBodyWrapper>div').css("height", fixRig);
    $('.DTFC_ScrollWrapper').css("height", mainHei);
    // var hh=$('.DTFC_ScrollWrapper').height()-36+7;
    // $('.dataTables_scrollBody').css("height",hh);
}

function jumpPage(e, oTableId) {
    var oTable = $("#" + oTableId).dataTable();
    var page = Number($(e).val());
    var max = $(e).attr("max");
    var number = /^[0-9][0-9]*$/
    if (!number.test(page)) {
        alert("您输入的页码不能识别，请输入1 - " + max + " 的整数页码");
        return false;
    } else if (page >= 1 && page <= max) {
        oTable.api().page(page - 1).draw(false);
    } else {
        alert("您输入的页码不在范围之内，请输入1 - " + max);
    }
}